﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:infConverters="clr-namespace:StockTraderRI.Infrastructure.Converters;assembly=StockTraderRI.Infrastructure"
    x:Class="StockTraderRI.Modules.Position.PositionGrid" >
    <UserControl.Resources>
        <infConverters:TwoDecimalPlaceConverter x:Key="twoDecimalPlaceConverter" />
        <Style x:Key="HeaderContainerStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="FontFamily" Value="Franklin Gothic"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="Background" Value="#FF77B6EB"/>
            <Setter Property="Foreground" Value="#FFFFFFFF"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Gray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <ListView x:Name="_positionTable" BorderThickness="0" Width="460" HorizontalAlignment="Left"
                  ItemsSource="{Binding Path=PositionSummaryItems}" 
                  SelectionChanged="_positionTable_SelectionChanged" 
                  IsSynchronizedWithCurrentItem="True">
        <ListView.Resources>
            <ContextMenu x:Name="positionGridContextMenu" x:Key="ContextMenuKey" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}">
                <MenuItem x:Name="BuyMenuItem" Header="Buy" Command="{Binding Path=DataContext.BuyCommand}" CommandParameter="{Binding Path=SelectedItem.TickerSymbol}"></MenuItem>
                <MenuItem x:Name="SellMenuItem" Header="Sell" Command="{Binding Path=DataContext.SellCommand}" CommandParameter="{Binding Path=SelectedItem.TickerSymbol}"></MenuItem>
            </ContextMenu>
        </ListView.Resources>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="ContextMenu" Value="{DynamicResource ContextMenuKey}"></Setter>
                <Setter Property="FontFamily" Value="Franklin Gothic"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter Property="Foreground" Value="#FF77B6EB"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Current positions"
                          ColumnHeaderContainerStyle="{StaticResource HeaderContainerStyle}">
                <GridViewColumn Header="Symbol" 
            		DisplayMemberBinding="{Binding Path=TickerSymbol}" 
            		Width="65"/>
                <GridViewColumn Header="Shares"  
                  DisplayMemberBinding="{Binding Path=Shares}"
                  Width="65" />
                <GridViewColumn Header="Last"  
                  DisplayMemberBinding="{Binding Path=CurrentPrice, Converter={StaticResource twoDecimalPlaceConverter}, ConverterParameter=2}"
                  Width="65" />
                <GridViewColumn Header="Cost Basis"  
                  DisplayMemberBinding="{Binding Path=CostBasis, Converter={StaticResource twoDecimalPlaceConverter}, ConverterParameter=2}"
                  Width="85" />
                <GridViewColumn Header="Market Value"  
                  DisplayMemberBinding="{Binding Path=MarketValue, Converter={StaticResource twoDecimalPlaceConverter}, ConverterParameter=2}"
                  Width="85" />
                <GridViewColumn Header="Gain Loss %"  
                  DisplayMemberBinding="{Binding Path=GainLossPercent, Converter={StaticResource twoDecimalPlaceConverter}, ConverterParameter=2}"
                  Width="85" />
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>